package com.service.impl;

import com.dao.StudentsDao;
import com.entity.Students;
import com.service.StudentsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.List;

@Service
public class StudentsServiceImpl implements StudentsService {

    @Resource
    private StudentsDao studentsDao;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean add(Students obj) throws Exception {
        return studentsDao.add(obj);
    }

    @Override
    public Students queryById(String id) {
        return studentsDao.queryById(id);
    }

    @Override
    public List<Students> queryAll() {
        return studentsDao.queryAll();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean update(Students obj) throws Exception {
        return studentsDao.update(obj);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean delete(String id) throws Exception {
        return studentsDao.delete(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addBatch(List<Students> objs) throws Exception {
        try {
            studentsDao.addBatch(objs);
        }catch(Exception ex){
            ex.printStackTrace();
            //出现了异常必须手动回顾。
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }
}
